package com.woniuxy.hackredis;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

//客户端发送消息
public class MyHackRedisClient {
    Socket socket;

    public MyHackRedisClient(){
        try {
            socket =  new Socket("127.0.0.1",6379);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void hset(String key,String field,String value) throws IOException {
        StringBuffer sb = new StringBuffer();
        sb.append("*4").append("\r\n");
        sb.append("$4").append("\r\n");
        sb.append("HSET").append("\r\n");
        sb.append("$").append(key.length()).append("\r\n");
        sb.append(key).append("\r\n");
        sb.append("$8").append("\r\n");
        sb.append("mayuhang").append("\r\n");
        sb.append("$").append(value.length()).append("\r\n");
        sb.append(value).append("\r\n");
        byte[] bytes = sb.toString().getBytes(StandardCharsets.UTF_8);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();
        socket.close();
    }
}
